home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / DIALOG_W / DIALOG.C
C/C++ Source or Header  |  1989-11-22  |  7KB  |  286 lines

  1.  
  2. /*
  3.  *  dialog.c
  4.  *
  5.  *  Copyright (c) 1989 Symantec Corporation.  All rights reserved.
  6.  *    by Darrell LeBlanc
  7.  */
  8.  
  9. /*    This is a demonstration of a scroll bar and a list in a dialog         */
  10. /*    window, keeps track of the value, and demonstrates the value to     */
  11. /*    the user. The DLOG resource it uses has three fields                */
  12. /*     If you don't have the .rsrc file, it has a DLOG ID #130 and a DITL    */
  13. /*    whose ID is 130. The DITL has four items:                            */
  14. /*     1. an enabled OK Button.                                            */
  15. /*     2. a text edit field to display the current value                    */
  16. /*    3. a user item 16 pixels wide, for the scroll bar to live in.         */
  17. /*    4. a user item used to display example of list in a dialog            */
  18.  
  19. /* item numbers of things in DITL & DLOG */
  20. #define OK_BUTTON        1
  21. #define CURRENT_VALUE    2
  22. #define SCROLL_BAR_ID    3
  23. #define LIST_ID            4
  24.  
  25. /* globals */
  26. ListHandle        OnListHand = 0L;
  27. Cell            OldCell;
  28. DialogPtr         theDialog;
  29. Boolean         WaitABit;
  30. ControlHandle     myScroll, theControl;
  31. int             value;
  32. int             myControlValue;
  33. Handle             iHndle;
  34. Rect            dataBounds;
  35. int                NumCells = 20;
  36.  
  37. /* prototypes */
  38. void OnLineClick(EventRecord myEvent);
  39. pascal void myDraw (DialogPtr theDialog, int theItem);
  40. pascal void ScrollProc (ControlHandle theControl, int thePart);
  41. pascal Boolean myFilter(DialogPtr theDialog, EventRecord *theEvent, int *itemHit);
  42. void DoOurDialog(void);
  43. int main(void);
  44.  
  45. /* Called when a mousedown event occurs in the list of topics in the dialog */
  46. void OnLineClick(EventRecord myEvent)
  47. {
  48.     Cell    lastCell;
  49.     OSErr    myErr;
  50.  
  51.     GlobalToLocal(&myEvent.where);
  52.     LClick(myEvent.where,myEvent.modifiers,OnListHand);
  53.     *(long *)&lastCell = LLastClick(OnListHand);
  54.     if(lastCell.v != OldCell.v)
  55.         OldCell.v = lastCell.v;
  56. } /* OnLineClick */
  57.  
  58. pascal void myDraw (DialogPtr theDialog, int theItem)
  59. {
  60.     DrawControls(theDialog);
  61. }
  62.     
  63. /* Handles hits in the control that are not the thumb. Adjusts             */
  64. /* the display of the current display value.                            */
  65. pascal void ScrollProc (ControlHandle theControl, int thePart)
  66. {
  67.     int     delta;
  68.     int     oldValue;
  69.     int     iType;
  70.     Rect     iBox;
  71.     Handle     iHndle;
  72.     int     value;
  73.     Str255     myString;
  74.     Boolean DoChange;
  75.     long     aLong;
  76.  
  77.     if (WaitABit)
  78.         Delay(10, &aLong);
  79.         
  80.     DoChange = true;
  81.     /* DebugStr("\pat switch"); */
  82.     switch (thePart)
  83.         {
  84.         case inUpButton: 
  85.             delta = -1;
  86.             if ((GetCtlValue(myScroll)) == (GetCtlMin(myScroll)))
  87.                 DoChange = false;
  88.             break;
  89.  
  90.         case inDownButton: 
  91.             delta = 1;
  92.             if ((GetCtlValue(myScroll)) == (GetCtlMax(myScroll)))
  93.                 DoChange = false;
  94.             break;
  95.             
  96.         case inPageUp: 
  97.             if (WaitABit)
  98.                 delta = -1;
  99.             else
  100.                 delta = -5;
  101.             break;
  102.             
  103.         case inPageDown: 
  104.             if (WaitABit)
  105.                 delta = 1;
  106.             else
  107.                 delta = 5;
  108.             break;
  109.         }    /* switch */
  110.         
  111.     WaitABit = false;
  112.     
  113.     if ((DoChange) && (thePart != 0))
  114.         {
  115.             oldValue = GetCtlValue(theControl);
  116.             SetCtlValue(theControl, oldValue + delta);
  117.         }
  118.  
  119.     GetDItem(theDialog, CURRENT_VALUE, &iType, &iHndle, &iBox);
  120.     value = GetCtlValue(theControl);
  121.     if (myControlValue != value)
  122.         {
  123.             NumToString(value, myString);
  124.             SetIText(iHndle, myString);
  125.             myControlValue = value;
  126.         }
  127. }
  128.  
  129.  
  130. pascal Boolean myFilter(DialogPtr theDialog, EventRecord *theEvent, int *itemHit)
  131. {
  132.     int iType;
  133.     Rect scroll_Box,list_Box;
  134.     Point mouseLoc;
  135.     Str255 myString;
  136.     int thePart;
  137.     Boolean DoTrack;
  138.     long along;
  139.     char    cr;
  140.  
  141.     WaitABit = true;
  142.  
  143.     switch (theEvent->what)
  144.         {
  145.         case keyDown:
  146.         case autoKey:                                 /* The default dialog dismissal.*/
  147.             {
  148.             cr = BitAnd(theEvent->message, charCodeMask);
  149.             if (( cr == 3) || (cr == 13))             /*<CR> or <ENTER>*/
  150.                 {
  151.                     GetDItem(theDialog, 1, &iType, &iHndle, &scroll_Box);
  152.                     HiliteControl(iHndle, 1);
  153.                     Delay(10, &along);
  154.                     *itemHit = 1;
  155.                     return true;
  156.                 }
  157.             }
  158.         case mouseDown: 
  159.             {
  160.             mouseLoc = theEvent->where;
  161.             GlobalToLocal(&mouseLoc);
  162.             GetDItem(theDialog, SCROLL_BAR_ID, &iType, &iHndle, &scroll_Box);/* scroll box*/
  163.             GetDItem(theDialog, LIST_ID, &iType,&iHndle, &list_Box);    /* list box */
  164.             if (PtInRect(mouseLoc, &scroll_Box))                 /*we know it is our scroll*/
  165.                 {
  166.                 /*Was mouse pressed in a control?*/
  167.                 thePart = FindControl(mouseLoc, theDialog, &theControl);
  168.                 if (theControl == myScroll)
  169.                     {
  170.                     if (thePart == inThumb)
  171.                         {
  172.                         /*Cannot use filter with thumb.*/
  173.                         thePart = TrackControl(myScroll, mouseLoc, 0L);
  174.                         
  175.                         /*Get and display the new value.*/
  176.                         myControlValue = GetCtlValue(myScroll);    
  177.                         GetDItem(theDialog, CURRENT_VALUE, &iType, &iHndle, &scroll_Box);
  178.                         NumToString(myControlValue, myString);
  179.                         SetIText(iHndle, myString);
  180.                         myControlValue = value;
  181.                         }
  182.                     else
  183.                         /*Handled in ScrollProc.*/
  184.                         thePart = TrackControl(myScroll, mouseLoc, ScrollProc);
  185.                     } /* if (theControl == myScroll) */
  186.                 } /* if PtInRect */
  187.             else if (PtInRect(mouseLoc, &list_Box))        /* mousedown evt in list */
  188.                 OnLineClick(*theEvent);
  189.             } /* case mouseDown */
  190.     }    /* switch */
  191. } /* myFilter */
  192.  
  193. void DoOurDialog()
  194. {
  195.     int         iType;
  196.     Rect         iBox;
  197.     Handle         iHndle;
  198.     int         itemNumber;
  199.     GrafPtr     savePort;
  200.     Str255         myString;
  201.     int            i;
  202.     Cell        theCell;
  203.     Rect    viewRect,cntrlRect,rView,box,dataBounds;
  204.     Point    cSize;
  205.     int    itemtype;
  206.     
  207.     theDialog = GetNewDialog(130,0L,-1L);
  208.  
  209.     GetPort(&savePort);
  210.     SetPort(theDialog);
  211.     
  212.     /* frame the default OK button */
  213.     GetDItem(theDialog, OK_BUTTON, &iType, &iHndle, &iBox);
  214.     InsetRect(&iBox, -5, -5);
  215.     PenSize(3, 3);
  216.     FrameRoundRect(&iBox, 16, 16);
  217.     PenNormal();
  218.  
  219.     /* set up the demo scroll bar */
  220.     GetDItem(theDialog, SCROLL_BAR_ID, &iType, &iHndle, &iBox);
  221.     SetDItem(theDialog, SCROLL_BAR_ID, iType, myDraw, &iBox);
  222.     myScroll = NewControl(theDialog, &iBox, "\pmyScroll", true, myControlValue, 0, 100, 16, 0);
  223.  
  224.     /* set up static text box to show value of scroll bar */
  225.     GetDItem(theDialog, CURRENT_VALUE, &iType, &iHndle, &iBox);
  226.     NumToString(myControlValue, myString);
  227.     SetIText(iHndle, myString);
  228.     myControlValue = value;
  229.     
  230.  
  231.     /* set up list mgr example */
  232.     GetDItem(theDialog,LIST_ID,&itemtype,&iHndle,&box);
  233.     rView = box;
  234.     rView.left+=1;        /* leave room for framerect */
  235.     rView.right-=16;    /* room for scrool bar and framerect */
  236.     rView.bottom-=1;    /* room for framerect */
  237.     cSize.h = rView.right - rView.left;
  238.     cSize.v = (rView.bottom - rView.top)/12;  /* show 12 cells at a time */
  239.     /* to make it look good, you REALLY should set the height of each cell */
  240.     /* to be an even divisor of the total height of the user item box height */
  241.     /* but it is late, and I don't feel like figuring it out... */
  242.     
  243.     SetRect(&dataBounds,0,0,1,NumCells);
  244.  
  245.     OnListHand = LNew(&rView,&dataBounds,cSize,0,theDialog,true,false,false,true);
  246.     (**OnListHand).selFlags = lOnlyOne+lNoDisjoint+lNoExtend+lNoNilHilite;
  247.  
  248.     for(i=0;i<NumCells;i++)
  249.         {
  250.         SetPt(&theCell,0,i);
  251.         LClrCell(theCell,OnListHand);
  252.         LSetCell("foo",3,theCell,OnListHand);
  253.         }
  254.     
  255.     GetDItem(theDialog, LIST_ID, &itemtype, &iHndle, &box);
  256.     box.top-=1;
  257.     FrameRect(&box);
  258.  
  259.     ModalDialog(myFilter, &itemNumber);
  260.     while(itemNumber != 1)
  261.         ModalDialog(myFilter,&itemNumber);
  262.  
  263.  
  264.     myControlValue = GetCtlValue(myScroll);
  265.  
  266.     SetPort(savePort);
  267.     DisposeControl(myScroll);
  268.     DisposDialog(theDialog);
  269. }    /* DoOurDialog */
  270.  
  271. main()
  272. {    
  273.  
  274.     InitGraf(&thePort);
  275.     InitFonts();
  276.     FlushEvents( everyEvent, 0 );
  277.     InitWindows();
  278.     InitMenus();
  279.     TEInit();
  280.     InitDialogs(0L);
  281.     InitCursor();
  282.     MaxApplZone();
  283.  
  284.     myControlValue = 55;
  285.     DoOurDialog();
  286. }